package com.java.cloud.storage.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.java.cloud.storage.entity.Storage;
import com.java.cloud.storage.service.StorageService;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;


/**
 * 库存
 *
 * @author jl
 * @since 2020-03-13 11:44:37
 */
@RestController
@RequestMapping("/storage")
public class StorageController {
	@Autowired
	private StorageService storageService;

	/**
	 * 扣减库存
	 */
	@PostMapping("/decrease")
	public R decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count) {
		return storageService.decrease(productId, count) == 1 ? R.ok() : R.error("扣减库存失败!");
	}

	/**
	 * 分页查询storage
	 * 当前页码page、每页显示记录数limit
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = storageService.queryPage(params);

		return R.ok(page);
	}


	/**
	 * 根据主键查询storage
	 */
	@GetMapping("/info/{int}")
	public R info(@PathVariable("id") Long id) {
		Storage storage = storageService.getById(id);

		return R.ok(storage);
	}

	/**
	 * 新增storage
	 */
	@PostMapping("/save")
	public R save(@RequestBody Storage storage) {
		storageService.save(storage);

		return R.ok();
	}

	/**
	 * 修改storage
	 */
	@PutMapping("/update")
	public R update(@RequestBody Storage storage) {
		storageService.updateById(storage);

		return R.ok();
	}

	/**
	 * 删除storage
	 */
	@DeleteMapping("/delete")
	public R delete(@RequestBody Long[] ints) {
		storageService.removeByIds(Arrays.asList(ints));

		return R.ok();
	}

}
